package jp.sourceforge.akjrcp.text_douga.item;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;

import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.DougaSystem;
import jp.sourceforge.akjrcp.text_douga.ImageObject;
import jp.sourceforge.akjrcp.text_douga.SystemEnvironment;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
 * normal image
 */
public class ImageItem extends AbstractItem{
	public static Log log=LogFactory.getLog(ImageItem.class);
	
	protected String inputPath;
	
	
	public String toString(){
		return "ImageItem:"+super.toString()+" inputPath="+inputPath;
	}
		//location
	public int getZ() {
		// small is firster
		return z;
	}

	public boolean writeFrame(DougaPreference preference,ImageObject baseImage,int frame)throws IOException{
		boolean written=false;
		if(duration==DougaSystem.FOREEVER){//maybe this is background image
			written=true;
		}
		double frameTime=(double)(frame-1)/preference.getFps();//to change start 0
		if(written==false){
		
		if(frameTime>=startTime && frameTime<startTime+duration){
			log.info("intime:"+frameTime+" start="+startTime+" end="+(startTime+duration));
			written=true;
			}
		}
		
		if(written){
			//drawing;
			Rectangle drawRect=getDrawRect(preference);
			BufferedImage img=SystemEnvironment.getSystemEnvironment().getImageCasher().loadImage(inputPath);
			log.info(drawRect);
			if(img==null){
				log.warn("img null:"+inputPath);
			}
			log.info(img.getWidth()+"x"+img.getHeight());
			Graphics2D g2=(Graphics2D) baseImage.getImage().getGraphics();
			float tp=getFrameTransparent(frameTime);
			log.info("transparent="+tp);
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tp));//use tp!
			g2.drawImage(img, drawRect.x, drawRect.y, drawRect.x+drawRect.width, drawRect.y+drawRect.height, 0, 0, img.getWidth(), img.getHeight(), null);
		}
		
		return written;
	}


	public Rectangle getDrawRect(DougaPreference preference){
		BufferedImage img=SystemEnvironment.getSystemEnvironment().getImageCasher().loadImage(inputPath);
		if(img==null){
			log.warn("img is null");
			return null;
		}
		int dw=width;
		int dh=height;
		
		
		if(dw!=0 && dh!=0 && keepAspect){//keep aspect
			double waspect=(double)dw/img.getWidth();
			double haspect=(double)dh/img.getHeight();
			if(waspect>haspect){//fit h
				dw=(int) (img.getWidth()*haspect);
				
			}else{
				dh=(int)(img.getHeight()*waspect);
			}
			log.info("waspect="+waspect+" haspect="+haspect+" dw="+dw+" dh="+dh);
		}
		
		
		if(dw==0){
			dw=img.getWidth();
		}
		
		if(dh==0){
			dh=img.getHeight();
		}
		
		int dx=0;
		if(align==NONE){
			dx=x;
		}else{
			if(align==LEFT){
				dx=0;
			}else if(align==RIGHT){
				dx=preference.getWidth()-dw;
			}else if(align==CENTER){
				dx=(preference.getWidth()-dw)/2;
			}else{
				log.warn("invalid align:"+align);
			}
		}
		int dy=0;
		if(valign==NONE){
			dy=y;
		}else{
			if(valign==TOP){
				dy=0;
			}else if(valign==BOTTOM){
				dy=preference.getHeight()-dh;
			}else if(valign==MIDDLE){
				dy=(preference.getHeight()-dh)/2;
			}else{
				log.warn("invalid valign:"+valign);
			}
		}
		return new Rectangle(dx,dy,dw,dh);
	}

	public String getInputPath() {
		return inputPath;
	}

	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}


	
}
